using System;
using System.Windows;
using System.Data;
using System.Xml;
using System.Configuration;

namespace C4F.DevKit.RSS.ScreenSaverWPF
{
    public partial class App : System.Windows.Application
    {
        public void OnApplicationStartup(object sender, StartupEventArgs args)
        {
            ScreenSaverArgument arg = 
                ParseScreenSaverArguments(System.Environment.GetCommandLineArgs());

            // Supporting the ability to show the screen saver and options window.
            switch (arg)
            {
                case ScreenSaverArgument.ShowScreenSaver:
                    MainWindow mainWindow = new MainWindow();
                    mainWindow.Show();
                    break;
                case ScreenSaverArgument.ShowPreview:
                    break;
                case ScreenSaverArgument.ShowOptions:
                    OptionsWindow optWindow = new OptionsWindow();
                    optWindow.Show();
                    break;
                case ScreenSaverArgument.ShowDebugMode:
                    break;
                case ScreenSaverArgument.Invalid:
                    break;
            }
        }

        private ScreenSaverArgument ParseScreenSaverArguments(string[] args)
        {
            if (args == null)
            { throw new ArgumentNullException("args"); }

            ScreenSaverArgument returnArg = ScreenSaverArgument.Invalid;

            if (args.Length > 1)
            {
                // Get the 2 character command line argument
                string arg = args[1].ToLowerInvariant().Trim().Substring(0, 2);
                switch (arg)
                {
                    case "/c":
                        // Show the options dialog
                        returnArg = ScreenSaverArgument.ShowOptions;
                        break;
                    case "/p":
                        returnArg = ScreenSaverArgument.ShowPreview;
                        break;
                    case "/s":
                        // Show screensaver form
                        returnArg = ScreenSaverArgument.ShowScreenSaver;
                        break;
                    case "/d":
                        // Show screensaver in debug mode
                        returnArg = ScreenSaverArgument.ShowDebugMode;
                        break;
                    default:
                        returnArg = ScreenSaverArgument.Invalid;
                        //MessageBox.Show("Invalid command line argument: " + arg, "Invalid Command Line Argument", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                }
            }
            else
            {
                // If no arguments were passed in, show the screensaver
                returnArg = ScreenSaverArgument.ShowScreenSaver;
            }

            return returnArg;
        }

        enum ScreenSaverArgument
        {
            Invalid,
            ShowOptions,
            ShowPreview,
            ShowScreenSaver,
            ShowDebugMode
        }

        /// <summary>
        /// Thrown in the event that the Interop.Feeds.dll cannot be located.
        /// </summary>
        [Serializable]
        public class NoInteropException : System.Exception
        {
            public NoInteropException() { }
            public NoInteropException(string message)
                : base(message) { }
            public NoInteropException(string message, Exception inner)
                : base(message, inner) { }
            protected NoInteropException(
                    System.Runtime.Serialization.SerializationInfo info,
                    System.Runtime.Serialization.StreamingContext context)
                : base(info, context) { }
        }
    }
}